package com.wxdc.nmgyt.nmgytwydc.presenter;

import com.wxdc.nmgyt.nmgytwydc.base.RxPersenter;
import com.wxdc.nmgyt.nmgytwydc.http.RetrofitHelper;
import com.wxdc.nmgyt.nmgytwydc.model.CodeBean;
import com.wxdc.nmgyt.nmgytwydc.model.backresult.UserRolResultBean;
import com.wxdc.nmgyt.nmgytwydc.presenter.contract.MainContract;

import java.util.List;

import javax.inject.Inject;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by Administrator on 2018/4/19.
 */

public class MainPersenter extends RxPersenter<MainContract.View>  implements MainContract.Persenter{
    private RetrofitHelper mRetrofitHelper;

    @Inject //将我们需要注入的对象的类的构造参数使用@Inject标注，告诉dagger2它可以实例化这个类
    public MainPersenter(RetrofitHelper retrofitHelper){
        this.mRetrofitHelper = retrofitHelper;
    }

    @Override
    public void RequestUserRol(String sid) {
        mRetrofitHelper.userRolQueryService(sid)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<UserRolResultBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        addDisposable(d);
                    }

                    @Override
                    public void onNext(UserRolResultBean userRolResultBean) {
                        mView.UserRolResult(userRolResultBean);
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.showError("请求失败,请检查网络接口");
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
    @Override
    public void RequestCode() {
        mRetrofitHelper.reuqestAllCodeService()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<List<CodeBean>>() {
                    @Override
                    public void onSubscribe(Disposable disposable) {
                        addDisposable(disposable);
                    }

                    @Override
                    public void onNext(List<CodeBean> resultBean) {
                        mView.CodeTabResult(resultBean);
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.showError("请求失败,请检查网络接口");
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}
